/**
 * 发送get请求
 * @param{String} url 请求地址
 * @param{String} param 请求参数(参数名=值,...,参数名=值)
 * @param{Function} headersFn 自定义请求头
 * @returns Promise
 */
function get(url, params, headersFn) {
  return new Promise((resolve, reject) => {
    const xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
      if (xhr.readyState == 4) {
        if (xhr.status == 200) {
          let rs = JSON.parse(xhr.responseText);
          resolve(rs);
        } else {
          reject(xhr.status);
        }
      }
    };
    xhr.open("get", `${url}?${params}`);
    if (headersFn) {
      headersFn(xhr);
    }
    xhr.send(null);
  });
}
/**
 * 发送post请求
 *
 */
function post(url, params, headersFn = null) {
  return new Promise((resolve, reject) => {
    const xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
      if (xhr.readyState == 4) {
        if (xhr.status == 200) {
          let rs = JSON.parse(xhr.responseText);
          resolve(rs);
        } else {
          reject(xhr.status);
        }
      }
    };
    xhr.open("post", url);

    xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded");
    if (headersFn) headersFn(xhr);

    xhr.send(params);
  });
}
